//
// File:    loadprogdlg.cpp
// Brief:   测试程序载入对话框实现文件
//
// History:
// ============================================================================
// 2011-01-20    Finux Chen <finuxchen@gmail.com>    创建
//
//
#include <QtGui>
#include "loadprogdlg.h"

LoadProgDlg::LoadProgDlg(QWidget *parent)
    : QDialog(parent)
{
	ui.setupUi(this);
	view	     = ui.tableView;
	fileLabel    = ui.fileLabel;
	okButton     = ui.okButton;
	cancelButton = ui.cancelButton;

    model = new QFileSystemModel;
    // model监视的目录,其下的修改会立刻signal通知view
    model->setRootPath(QDir::currentPath());
    // 仅显示文件
    model->setFilter(QDir::Files);

//    view = new QTableView;
    view->setModel(model);
    // 设置view显示的目录
    view->setRootIndex(model->index(QDir::currentPath()));

    // 隐藏垂直表头
    view->verticalHeader()->setHidden(true);
    // 隐藏文件类型,文件大小两栏
    view->setColumnHidden(1, true);
    view->setColumnHidden(2, true);
    // 隐藏网格线
    view->setShowGrid(false);

    view->setColumnWidth(0, 350);
    view->setColumnWidth(3, 350);

/*
	QModelIndex index;
	for(int r = 0; r < model->rowCount(); ++r){
		index = model->index(r, 0);
		if(!model->fileName(index).contains(tr(".PRG"), Qt::CaseInsensitive)){
			view->setRowHidden(index.row(), true);
		}
	}
*/

    // 单行选择模式
    view->setSelectionBehavior(QAbstractItemView::SelectRows);
    view->setSelectionMode(QAbstractItemView::SingleSelection);

	// set font size
	QFont font = this->font();
	font.setPixelSize(font.pointSize() * 4 / 3);
	setFont(font);

	setWindowFlags(Qt::CustomizeWindowHint | Qt::FramelessWindowHint);
	showMaximized();

    connect(okButton, SIGNAL(clicked()), this, SLOT(dialogShouldReturn()));
    connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
	connect(view, SIGNAL(pressed(const QModelIndex &)), 
					this, SLOT(pathSelected(const QModelIndex &)));
}


LoadProgDlg::~LoadProgDlg()
{
	delete model;
	delete view;
	delete okButton;
	delete cancelButton;
	delete fileLabel;
}

QString LoadProgDlg::getFilePath()
{
	return fileLabel->text();
}

void LoadProgDlg::pathSelected(const QModelIndex &index)
{
	if(!index.isValid()){
		return;
	}

	QModelIndex idx = index.sibling(index.row(), 0);
//	editFilePath->setText(model->fileName(idx));
	fileLabel->setText(model->fileName(idx));
}



void LoadProgDlg::dialogShouldReturn()
{
	if(fileLabel->text() == ""){
		return;
	}
	accept();
}
